//
//  XJChildCommentCell.m
//  XinJiangMall
//
//  Created by RaoBo on 2018/6/7.
//  Copyright © 2018年 Tzyang. All rights reserved.
//

#import "XJChildCommentCell.h"

//model
#import "XJTieZiDetailModel.h"
#import "NSAttributedString+YYText.h"


@implementation XJChildCommentCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


#pragma mark - 一 初始化
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        [self setupView];
        
//        self.backgroundColor = [UIColor greenColor]; // ✅
    }
    
    return self;
}

- (void)setupView {
    [self.contentView addSubview:self.commentLabel];
    
    // 65 的 说明
    // |-15的间距-|-------|-------|------|
    /**
     
     |--15的间距--|--icon40--|-间距5-|----commentLabel的长度为x-----|-间距5-|
     
     x = kWidth - 65(15+40+5+5)
     */
    // 高度不给
    self.commentLabel.sd_layout
    .leftSpaceToView(self.contentView, 5)
    .topSpaceToView(self.contentView, 5)
    .rightSpaceToView(self.contentView, 5);
}

#pragma mark - 二 lazy
- (YYLabel *)commentLabel {
    if (!_commentLabel) {
        _commentLabel = [YYLabel new];
        _commentLabel.font = XJFONTSET_R(14.0);
        _commentLabel.numberOfLines = 0;
        _commentLabel.preferredMaxLayoutWidth = self.contentView.bounds.size.width;  //设置最大的宽度
        _commentLabel.displaysAsynchronously = YES; /// enable async display
    }
    return _commentLabel;
}

#pragma mark - 三 赋值
- (void)setChildCommentModel:(XJTieZiDetailChildCommentModel *)childCommentModel {
    _childCommentModel = childCommentModel;
    
    
    UIColor *blueColor = [UIColor blueColor];
    UIColor *grayColor = [UIColor lightGrayColor];
    UIColor *clearColor = [UIColor clearColor];
    
    // "张三" 回复  “李四” : xxxxxxxx
    if ([childCommentModel.level isEqualToString:@"3"]) {

        NSString *str = [NSString stringWithFormat:@"%@ 回复 %@:%@ %@",childCommentModel.nickname,childCommentModel.target_nickname,childCommentModel.text,childCommentModel.publish_time];
        // 宽度固定才能动态计算高度
        // 宽度先写死之后在调试
        CGFloat h = [self heightForStr:str];
        
        // 设置高度:
        self.commentLabel.sd_layout.heightIs(h);
        
        
        // 求范围
        NSRange range1 = [str rangeOfString:childCommentModel.nickname];
        NSRange range2 = [str rangeOfString:childCommentModel.target_nickname];
        NSRange range3 = [str rangeOfString:childCommentModel.publish_time];
        
        
        // 富文本赋值
        NSMutableAttributedString *mAttributeStr = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        mAttributeStr.yy_font = [UIFont systemFontOfSize:14.0];
        mAttributeStr.yy_lineSpacing = 0;
        mAttributeStr.yy_alignment = NSTextAlignmentJustified;

        [mAttributeStr yy_setTextHighlightRange:range1 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XJLog(@"1111");
        }];
        
        [mAttributeStr yy_setTextHighlightRange:range2 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XJLog(@"222");
        }];
        
        [mAttributeStr yy_setTextHighlightRange:range3 color:grayColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XJLog(@"3333");
        }];
        
        self.commentLabel.attributedText = mAttributeStr;
        
    }else {
        // “李四”: xxxxxxxxxx
        
        NSString *commentStr = [NSString stringWithFormat:@"%@:%@ %@",childCommentModel.target_nickname,childCommentModel.text,childCommentModel.publish_time];
        CGFloat h = [self heightForStr:commentStr]; // 计算高度
        
        self.commentLabel.sd_layout.heightIs(h);
        
        // 求范围
        NSRange range2 = [commentStr rangeOfString:childCommentModel.target_nickname];
        NSRange range3 = [commentStr rangeOfString:childCommentModel.publish_time];
        
        
        // 富文本赋值
        NSMutableAttributedString *mAttributeStr = [[NSMutableAttributedString alloc] initWithString:commentStr attributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}];
        mAttributeStr.yy_font = [UIFont systemFontOfSize:14.0];
        mAttributeStr.yy_lineSpacing = 0;
        mAttributeStr.yy_alignment = NSTextAlignmentJustified;
        
        [mAttributeStr yy_setTextHighlightRange:range2 color:blueColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XJLog(@"222");
        }];
        
        [mAttributeStr yy_setTextHighlightRange:range3 color:grayColor backgroundColor:clearColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
            XJLog(@"3333");
        }];
        
        self.commentLabel.attributedText = mAttributeStr;
    }
}

#pragma mark - private method
- (CGFloat)heightForStr:(NSString *)str {
    CGFloat maxWidth = KWidth -65;
    
    YYLabel *label = [YYLabel new];
    label.text = str;
    label.font = [UIFont systemFontOfSize:14.0];
    label.numberOfLines = 0;

    CGSize size = [label sizeThatFits:CGSizeMake(maxWidth, CGFLOAT_MAX)];
    return size.height;
}

// 富文本


@end
